package com.zz.ecommerce.declareManage.common.service;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.bean.insp.db.payment.TPaymentHeadInsp;
import com.zz.ecommerce.common.bean.qdc.db.TPaymentHeadQdc;
import com.zz.ecommerce.common.constants.DictConstants;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.PayBillInspDao;
import com.zz.ecommerce.declareManage.common.dao.PayBillQdcDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by admin on 2016/3/29.
 */
@Service
public class PayBillQdcService {

    @Autowired
    private PayBillQdcDao payBillQdcDao;
    @Autowired
    private PayBillInspDao payBillInspDao;


    public List<TPaymentHeadQdc> queryPayBillList(GridRequest request, TPaymentHeadQdc tPaymentHeadQdc, String ieFlag, String tradeMode) {

        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_QDC TOH WHERE TOH.ORDER_ID=T_PAYMENT_HEAD_QDC.ORDER_ID " +
                " AND T_PAYMENT_HEAD_QDC.EB_PLAT_ID = TOH.EB_PLAT_ID " +
                "            AND TOH.BUSINESS_TYPE='" + DictConstants.B2C + "'";
        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition += " AND TOH.TRADE_MODE='" + tradeMode + "'";
        }if (StringUtils.isNotEmpty(ieFlag)) {
            whereCondition += " AND TOH.IE_FLAG='" + ieFlag + "'";
        }
        whereCondition += ")";
        return payBillQdcDao.pageWhere(TableConstants.T_PAYMENT_HEAD_QDC, tPaymentHeadQdc, request.getStart(), request.getEnd(), request.getSortOrder(),
                whereCondition);
    }


    public int countBill(TPaymentHeadQdc tPaymentHeadQdc, String ieFlag, String tradeMode) {
        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_QDC TOH WHERE TOH.ORDER_ID=T_PAYMENT_HEAD_QDC.ORDER_ID " +
                " AND T_PAYMENT_HEAD_QDC.EB_PLAT_ID = TOH.EB_PLAT_ID " +
                "            AND TOH.BUSINESS_TYPE='" + DictConstants.B2C + "'";
        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition += " AND TOH.TRADE_MODE='" + tradeMode + "'";
        }
        if (StringUtils.isNotEmpty(ieFlag)) {
            whereCondition += " AND IE_FLAG='" + ieFlag + "'";
        }
        whereCondition += ")";
        return payBillQdcDao.countWhere(TableConstants.T_PAYMENT_HEAD_QDC, tPaymentHeadQdc,
                whereCondition);
    }


    public List<TPaymentHeadInsp> queryPayBillListInsp(GridRequest request, TPaymentHeadInsp tPaymentHeadInsp, String ieFlag, String tradeMode) {

        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_INSP TOH WHERE TOH.ORDERNO =" +
                "                T_PAYMENT_HEAD_INSP.ORDERNO\n" +
                "        AND T_PAYMENT_HEAD_INSP.ECPCODE = TOH.ECPCODE ";

        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition+= " AND TOH.BIZTYPE in (" + tradeMode.replaceAll("#","") + ") ";
        }
        if (StringUtils.isNotEmpty(ieFlag)) {
            whereCondition += " AND TOH.IETYPE='" + ieFlag + "'";
        }
        whereCondition += ")";
        return payBillInspDao.pageWhere(TableConstants.T_PAYMENT_HEAD_INSP, tPaymentHeadInsp, request.getStart(), request.getEnd(), request.getSortOrder(),
                whereCondition);
    }


    public int countBillInsp(TPaymentHeadInsp tPaymentHeadInsp, String ieFlag, String tradeMode) {
        String whereCondition = " AND EXISTS(SELECT 1 FROM T_ORDER_HEAD_INSP TOH WHERE TOH.ORDERNO =" +
                "                T_PAYMENT_HEAD_INSP.ORDERNO\n" +
                "        AND T_PAYMENT_HEAD_INSP.ECPCODE = TOH.ECPCODE ";

        if (StringUtils.isNotEmpty(tradeMode)) {
            whereCondition+= " AND TOH.BIZTYPE in (" + tradeMode.replaceAll("#","") + ") ";
        }
        if (StringUtils.isNotEmpty(ieFlag)) {
            whereCondition += " AND TOH.IETYPE='" + ieFlag + "'";
        }
        whereCondition += ")";
        return payBillInspDao.countWhere(TableConstants.T_PAYMENT_HEAD_INSP, tPaymentHeadInsp,
                whereCondition);
    }


}
